package com.example.lxh.testinfo;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import adapter.LivePagerAdapter;
import database.LivesqliteUtil;
import jsonbean.Qualitys;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class liveWeatherActivity extends AppCompatActivity implements View.OnClickListener {

    private ViewPager m_live_pager;
    private TextView m_tv0;
    private TextView m_tv1;
    private TextView m_tv2;
    private TextView m_tv3;

    private View view1;
    private View view2;
    private View view3;
    private View view4;

    private static List<Qualitys> listQ = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_weather);
        initView();
        List<View> list = new ArrayList<>();
        list.add(view1);
        list.add(view2);
        list.add(view3);
        list.add(view4);
        m_live_pager.setCurrentItem(0);
        m_live_pager.setAdapter(new LivePagerAdapter(list, this));
        Timer timer = new Timer();
        timer.schedule(new TimerTest(), 1000, 2000);
    }

    class TimerTest extends TimerTask {
        @Override
        public void run() {
            getData();
        }
    }

    public void getData() {
        final LivesqliteUtil util = new LivesqliteUtil(this);
        String urls = "http://111.231.195.53/TransportService/type/jason/action/GetAllSense.do";
        OkHttpClient client = new OkHttpClient();
        final Request request = new Request.Builder().url(urls).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e("Lxh", "onFailure: " + e
                );
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                Qualitys qualitys = null;
                try {
                    //  {"pm2.5":172,"temp":6,"co2":644,"LightIntensity":379,"humidity":57}
                    qualitys = new Qualitys();
                    JSONObject jsonObject = new JSONObject(str);
                    qualitys.setPm((Integer) jsonObject.get("pm2.5"));
                    qualitys.setCo2((Integer) jsonObject.get("co2"));
                    qualitys.setTemp((Integer) jsonObject.get("temp"));
                    qualitys.setLight((Integer) jsonObject.get("LightIntensity"));
                    qualitys.setHumidity((Integer) jsonObject.get("humidity"));
                    Log.w("Lxh", "qualitys: " + qualitys.toString());
                    listQ.add(qualitys);
                    Log.w("Lxh", "list size " + listQ.size());
                    if (listQ.size() == 20) {
                        Log.w("Lxh", "数据插入 数据库");
                        util.InsertData(listQ);
                        listQ.clear();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    private void initView() {
        m_live_pager = findViewById(R.id.live_pager);
        m_tv0 = findViewById(R.id.tv_0);
        m_tv1 = findViewById(R.id.tv_1);
        m_tv2 = findViewById(R.id.tv_2);
        m_tv3 = findViewById(R.id.tv_3);
        m_tv0.setOnClickListener(this);
        m_tv1.setOnClickListener(this);
        m_tv2.setOnClickListener(this);
        m_tv3.setOnClickListener(this);
        LayoutInflater lf = getLayoutInflater().from(this);
        view1 = lf.inflate(R.layout.liveitemlayout, null);
        view2 = lf.inflate(R.layout.liveitemlayout, null);
        view3 = lf.inflate(R.layout.liveitemlayout, null);
        view4 = lf.inflate(R.layout.liveitemlayout, null);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_0:

                break;
            case R.id.tv_1:

                break;
            case R.id.tv_2:

                break;
            case R.id.tv_3:

                break;
        }
    }
}
